package com.king.headline.util;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.google.gson.Gson;
import com.king.headline.UI.LogingPromoteActivity;
import com.king.headline.UI.MainUI;
import com.king.headline.beans.UserInfo;
import com.king.headline.cache.CacheUtils;

/**
 * Created by king on 2015/12/02.
 */
public class AccountManager {

    private UserInfo mUser;
    private Gson gson;

    private static AccountManager sInstance;
    private AccountManager() {
        gson = new Gson();
    }
    public static AccountManager getInstance() {
        if (sInstance == null) {
            synchronized ( AccountManager.class) {
                if (sInstance == null)
                    sInstance = new  AccountManager();

            }
        }
        return sInstance;
    }
    public void logout() {
        mUser = null;
        sInstance = null;
//        CacheUtils.putBoolean(CacheUtils.AUTO_ENTER, false);
        CacheUtils.putString(CacheUtils.APIKEY, "");
        goLogingActivity();

    }
    /**
     * 获取用户信息
     * @return
     */
    public UserInfo getUser() {
        return mUser;
    }

    /**
     * 设置用户信息
     *
     * @param user
     */
    public void setUser(UserInfo user) {
        this.mUser = user;
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public boolean isLogin(final Activity mactivity) {
        if (mUser == null ) {// 登录失效
            DialogUtils.showHintDialogOne(mactivity, "请先登录", true, new DialogUtils.OnDialogListener() {
                @Override
                public void onOk(View v) {
                    Intent intent = new Intent( UIUtils.getContext(), LogingPromoteActivity.class);
                    intent.putExtra("canback", true);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    UIUtils.getContext().startActivity(intent);
                }

                @Override
                public void onCancel(View v) {

                }
            });
            return false;
        }
        return true;
    }


    public static void goLogingActivity(){
        Intent itent = new Intent( UIUtils.getContext(), LogingPromoteActivity.class);
        itent.putExtra("canback", true);
        itent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        UIUtils.getContext().startActivity(itent);
    }
    public  void goMainActivity(Activity mactivity){
        Intent itent = new Intent( UIUtils.getContext(), MainUI.class);
        mactivity.startActivity(itent);
        mactivity.finish();
    }
    public  void setCookie(String rawCookies){
        CacheUtils.putString(CacheUtils.RAWCOOKIES, rawCookies);
    }
    public  String getCookie(){
        return  CacheUtils.getString(CacheUtils.RAWCOOKIES, "");
    }


}
